using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace DomainComponents.Common
{
    [DomainComponent]
    [NonPersistentDc]
    public interface IAddressable
    {
        [DevExpress.ExpressApp.DC.Aggregated]
        [VisibleInListView(false)]
        IBaseAddress PrimaryAddress { get; set; }

        [CreateInstance]
        IBaseAddress CreateAddress();

        void Copy(IAddressable target);
    }

    [DomainLogic(typeof(IAddressable))]
    public class AddressableLogic
    {
        public static void AfterConstruction(IAddressable addressable)
        {
            if (addressable.PrimaryAddress == null)
            {
                addressable.PrimaryAddress = addressable.CreateAddress();
            }
        }
        public static void Copy(IAddressable source, IAddressable target)
        {
            if (target != null && source.PrimaryAddress != null && target.PrimaryAddress != null)
            {
                source.PrimaryAddress.CopyTo(target.PrimaryAddress);
            }
        }
    }


    [DomainComponent]
    [XafDefaultProperty("DisplayName")]
    public interface IBaseAddress
    {
        //[PersistentDc]
        string DisplayName { get; set; }
        void CopyTo(IBaseAddress targetAddress);
    }



    [DomainLogic(typeof(IBaseAddress))]
    public class BaseAddressLogic
    {
        public static void CopyTo(IBaseAddress address, IBaseAddress targetAddress)
        {
            targetAddress.DisplayName = address.DisplayName;
        }
    }

    [DomainComponent]
    public interface IGenericAddress : IBaseAddress
    {
        string Street1 { get; set; }
        string Street2 { get; set; }
        string City { get; set; }
        string State { get; set; }
        string Zip { get; set; }
        string Country { get; set; }
    }

    [DomainLogic(typeof(IGenericAddress))]
    public class GenericAddressLogic
    {
        private static void UpdateDisplayName(IGenericAddress address)
        {
            address.DisplayName = string.Format("{0} {1} {2} {3} {4} {5}", address.Street2, address.Street1, address.City, address.State,
                address.Zip, address.Country);
        }
        public static void CopyTo(IBaseAddress address, IBaseAddress targetAddress)
        {
            IGenericAddress source = address as IGenericAddress;
            IGenericAddress target = targetAddress as IGenericAddress;
            if (source != null && target != null)
            {
                target.Street1 = source.Street1;
                target.Street2 = source.Street2;
                target.City = source.City;
                target.State = source.State;
                target.Zip = source.Zip;
                target.Country = source.Country;
            }
        }
        public static void AfterChange_Street1(IGenericAddress address)
        {
            UpdateDisplayName(address);
        }
        public static void AfterChange_Street2(IGenericAddress address)
        {
            UpdateDisplayName(address);
        }
        public static void AfterChange_City(IGenericAddress address)
        {
            UpdateDisplayName(address);
        }
        public static void AfterChange_State(IGenericAddress address)
        {
            UpdateDisplayName(address);
        }
        public static void AfterChange_Zip(IGenericAddress address)
        {
            UpdateDisplayName(address);
        }
        public static void AfterChange_Country(IGenericAddress address)
        {
            UpdateDisplayName(address);
        }
    }

    //[DefaultClassOptions]
    ////[ImageName("BO_Contact")]
    ////[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
    ////[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
    ////[Persistent("DatabaseTableName")]
    //// Specify more UI options using a declarative approach (http://documentation.devexpress.com/#Xaf/CustomDocument2701).
    //public class IAddress : BaseObject
    //{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (http://documentation.devexpress.com/#Xaf/CustomDocument3146).
    //    public IAddress(Session session)
    //        : base(session)
    //    {
    //    }
    //    public override void AfterConstruction()
    //    {
    //        base.AfterConstruction();
    //        // Place your initialization code here (http://documentation.devexpress.com/#Xaf/CustomDocument2834).
    //    }
    //    //private string _PersistentProperty;
    //    //[XafDisplayName("My display name"), ToolTip("My hint message")]
    //    //[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
    //    //[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
    //    //public string PersistentProperty {
    //    //    get { return _PersistentProperty; }
    //    //    set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
    //    //}

    //    //[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
    //    //public void ActionMethod() {
    //    //    // Trigger a custom business logic for the current record in the UI (http://documentation.devexpress.com/#Xaf/CustomDocument2619).
    //    //    this.PersistentProperty = "Paid";
    //    //}
    //}
}
